<template>
	<view class="" style="background-color: #FFFFFF;">
		<!-- <uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
					title="xx项目公租房报名" @clickLeft="back" /> -->
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">xx项目公租房报名</block>
		</cu-custom>			
		<uni-row class="demo-uni-row">
			<uni-col :span="12">
				<view class="demo-uni-col">
					<uni-list>
						<uni-list-item  title="姓名" :rightText="person.name"></uni-list-item>
					</uni-list>
				</view>
			</uni-col>
			<uni-col :span="12">
				<view class="demo-uni-col">
					<uni-list>
						<uni-list-item  title="工龄" :rightText="person.workAge"></uni-list-item>
					</uni-list>
				</view>
			</uni-col>
		</uni-row>
		<uni-row class="demo-uni-row">
			<uni-col :span="12">
				<view class="demo-uni-col">
					<uni-list>
						<uni-list-item  title="部门" :rightText="person.apartment"></uni-list-item>
					</uni-list>
				</view>
			</uni-col>
			<uni-col :span="12">
				<view class="demo-uni-col">
					<uni-list>
						<uni-list-item  title="婚姻状况" :rightText="person.marry"></uni-list-item>
					</uni-list>
				</view>
			</uni-col>
		</uni-row>
		<uni-row class="demo-uni-row">
			<uni-col :span="12">
				<view class="demo-uni-col">
					<uni-list>
						<uni-list-item  title="地址" :rightText="person.address"></uni-list-item>
					</uni-list>
				</view>
			</uni-col>
			<uni-col :span="12">
				<view class="demo-uni-col">
					<uni-list>
						<uni-list-item  title="其他" :rightText="person.other"></uni-list-item>
					</uni-list>
				</view>
			</uni-col>
		</uni-row>
		<uni-row class="demo-uni-row">
			<uni-col :span="8">
				<view class="demo-uni-col">
					<view class="example-body">
						<uni-combox :candidates="scoringStandards" placeholder="计分标准" v-model="scoringStandard"></uni-combox>
					</view>
				</view>
			</uni-col>
			<uni-col :span="8">
				<view class="demo-uni-col">
					<view class="example-body">
						<uni-combox :candidates="qualifications" placeholder="资质" v-model="qualification"></uni-combox>
					</view>
				</view>
			</uni-col>
			<uni-col :span="8">
				<view class="demo-uni-col">
					<button type="button" class="button-add" @click="add">添加</button>
				</view>
			</uni-col>
		</uni-row>
		<uni-table ref="table" border stripe emptyText="暂无更多数据" >
			<!-- 表头行 -->
			<uni-tr>
				<uni-th width="10" align="center">序号</uni-th>
				<uni-th width="80" align="center">资质</uni-th>
				<uni-th width="30" align="left">得分</uni-th>
				<uni-th width="100" align="left">附件</uni-th>
				<uni-th width="60" align="left">操作</uni-th>
			</uni-tr>
			<!-- 表格数据行 -->
			<uni-tr v-for="(item, index) in tableData" :key="index">
				<uni-td>{{index+1}}</uni-td>
				<uni-td>{{ item.qualification }}</uni-td>
				<uni-td>{{ item.score }}</uni-td>
				<uni-td>
					<view class="grid col-4 grid-square flex-sub">
						<view class="bg-img" v-for="(item1,index1) in item.appendix" :key="index1" @tap="ViewImage(index,$event)" :data-url="item.appendix[index1]">
						 <image :src="imgUrl+item1" mode="aspectFill"></image>
							<view class="cu-tag bg-red radius" @tap.stop="DelImg(index,$event)" :data-index="index1">
								<text class='cuIcon-close'></text>
							</view>
						</view>
						<view class="solids" @tap="ChooseImage(index)" v-if="item.appendix.length<1">
							<text class='cuIcon-cameraadd'></text>
						</view>
					</view>
				</uni-td>
				<uni-td>
					<navigator url="javascript;" style="background-color: #00BFFF; color: #FFFFFF; text-align: center;">修改</navigator>
					<navigator url="javascript;" style="background-color: #00BFFF; color: #FFFFFF; text-align: center;">删除</navigator>
					<!-- <button type="button" class="button-common">与原件相符</button> -->
				</uni-td>
			</uni-tr>
		</uni-table>
		
		<view class="padding flex flex-direction">
			<view class="cu-form-group">
				<view class="title" style="margin-left: 60%;">总&nbsp;&nbsp;分：{{sum}}</view>
				<!-- <input placeholder="请输入标题" name="input" v-model="sum"></input> -->
			</view>
		</view>
		
		<view class="padding flex flex-direction">
			<button class="cu-btn bg-blue lg"  @click="upload()">提交审核</button>
		</view>
		
	</view>
</template>

<script>
	export default {
		name: 'signUp',
		data() {
			return {
				imgUrl: this.$config.staticDomainURL+'/',
				flag:0,
				tableData:[
					// {
					// 	qualification:'夫妻双方均未享受过优惠政策住房',
					// 	score: '10',
					// 	appendix: '',
					// }
				],
				person:{
					name: '史蒂文',
					workAge: '5',
					apartment: '信息部',
					marry: '已婚',
					address: 'xxx区xx',
					other: 'xxxxx',
				},
				scoringStandards: ['北京', '南京', '东京', '武汉', '天津', '上海', '海口'],
				qualifications: ['北京', '南京', '东京', '武汉', '天津', '上海', '海口'],
				qualificationsTotal: [],
				scoringStandard: '',
				qualification: '',
				value:[],
				imgList: [],
				uploadUrl:"/sys/common/upload",
				sum: 0,
			}
		},
		mounted() {
			console.log(this.$store.getters.userid)
			this.$http.get(`/base/znzfEmployee/searchByUid?userId=${this.$store.getters.userid}`).then(res => {
				console.log(1,res)
				let people = res.data.result;
				this.$http.get(`/sys/sysDepart/getDepartName?orgCode=${people.sysOrgCode}`).then(res => {
					// console.log(res.data.result.departName)
					this.person.name = people.name;
					this.person.workAge = people.seniority.toString();
					this.person.apartment = res.data.result.departName;
					this.person.marry = people.ismarried == 0 ? '未婚':'已婚';
					this.person.address = people.address;
					this.person.other = 'xxxxx';
				})
			})
			this.$http.get(`/base/znzfScoreStandard/list`).then(res => {
				// console.log(3,res)
				let temp = [];
				res.data.result.records.forEach(item => {
					temp.push(item.title)
				})
				this.scoringStandards = temp;
			})
			this.$http.get(`/base/znzfScoreStandard/queryZnzfScoreStandardItemByMainId?id=1508751655557005314`).then(res => {
				// console.log(4,res)
				let temp = [];
				res.data.result.forEach(item => {
					temp.push(item.name)
					this.qualificationsTotal.push({name:item.name,scoreStandardId:item.id,score:item.score})
				})
				this.qualifications = temp;
				// console.log(this.qualificationsTotal)
				this.$http.post(`/base/znzfEmployeeScoreItem/getEmployeeScoreItemList?userId=${this.$store.getters.userid}`).then(res => {
					// console.log(5,res.data.result)
					if (!res.data.result) {
						this.tableData = res.data.result
					}else {
						res.data.result.forEach(item => {
							let temp = {}
							// console.log(this.qualificationsTotal,item)
							temp.qualification = this.qualificationsTotal.filter(index => index.scoreStandardId == item.scoreItemId)[0].name
							temp.score = item.score
							if(item.appendix){
								temp.appendix = item.appendix.split(",")
							}else {
								temp.appendix = ""
							}
							this.tableData.push(temp)
						})
					}
					// console.log(this.tableData)
				})
				this.$http.post(`/base/znzfEmployeeScoreItem/queryLatestScoreByUserId?userId=${this.$store.getters.userid}`).then(res => {
					console.log(res.data.result)
					this.sum = res.data.result
				})
			})
		},
		computed:{
			// sum() {
			// 	return this.tableData.reduce((total,item) => (total += parseInt(item.score)),0)
			// }
		},
		methods: {
			signUp() {
				this.$Router.replace({name:'signUp'})
			},
			back() {
				this.$router.go(-1)
			},
			add() {
				let temp1 = this.qualification
				if(this.qualification) {
					let temp2 = this.qualificationsTotal.filter(item => item.name == temp1)[0].score
					this.tableData.push({qualification:temp1,
							score: temp2,
							appendix: '',})
				}
				console.log(this.tableData)
				this.sum = this.tableData.reduce((total,item) => (total += parseInt(item.score)),0)
			},
			upload(e) {
				
				let	znzfEmployeeScoreItemParamList=[]
				this.tableData.forEach(item => {
					let term = {};
					// console.log(item);
					term.userId = this.$store.getters.userid;
					let temp = this.qualificationsTotal.filter(index => index.name == item.qualification)[0]
					// console.log(temp)
					term.scoreItemId = temp.scoreStandardId
					term.score = temp.score
					if (item.appendix) {
						term.appendix = item.appendix.join(",")
					}else term.appendix = item.appendix
					
					znzfEmployeeScoreItemParamList.push(term)
				})
				console.log(znzfEmployeeScoreItemParamList)
				this.$http.post('/base/znzfEmployeeScoreItem/insertEmployeeScoreItemList',{znzfEmployeeScoreItemParamList}).then(res => {
					console.log(res)
				})
				console.log(this.sum.toString(),typeof this.sum.toString())
				this.$http.post(`/base/znzfEmployeeScoreItem/insertLatestScoreByUserId?userId=${this.$store.getters.userid}&latestScore=${this.sum.toString()}`).then(res => {
					console.log(res)
				})
			},
			ChooseImage(index) {
				console.log(index,this.tableData[index])
				var that=this;
				if (!that.tableData[index].appendix) {
					that.tableData[index].appendix = []
				}
				uni.chooseImage({
					count: 2, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						res.tempFilePaths.forEach(item => {
							that.$http.upload(that.$config.apiUrl+that.uploadUrl, {
								filePath: item,
								name: 'file'
							})
							.then(res => {
								console.log(res)
								that.tableData[index].appendix.push(res.data.message)
								console.log(that.tableData[index].appendix)
							})
							.catch(err => {
								that.$tip.error(err.data.message)
							});
						})
						// this.tableData[index].appendix = res.tempFilePaths
						// console.log(this.imgList)
					}
				});
			},
			ViewImage(index,e) {
				console.log(index,e,)
				let temp = []
				this.tableData[index].appendix.forEach(item => temp.push(this.$config.staticDomainURL+'/'+item))
				console.log(temp)
				uni.previewImage({
					urls: temp,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(index,e) {
				console.log(index,e)
				uni.showModal({
					title: this.person.name,
					content: '确定要删除吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							console.log(1,e.currentTarget.dataset.index)
							this.tableData[index].appendix.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.example-body{
		padding: 12px;
		background-color: #FFFFFF;
	}
	.button-add {
		margin-top: 10%;
		width: 60%;
		height: 60%;
		font-size: 14px;
	}
	.button-common,.button-common1 {
		// padding: 1px;
		background-color: #00BFFF;
		font-size: 14px;
		line-height: 14px;
	}
	.button-common1 {
		margin-top: 100px;
	}
	.blue /deep/.uni-list-item__content-title {
		color: #00BFFF;
	}
	.orange /deep/.uni-list-item__content-title {
		color: #F3A73F;
	}
	.red /deep/.uni-list-item__content-title {
		color: #ED1C24;
	}
	.button-choose{
		font-size: 14px;
		padding: 0;
	}
	.grid.col-4.grid-square>uni-view{
		padding-bottom: calc((100% - 1px)/2);
		height: 0;
		width: calc((100% - 1px)/2);
	}
</style>
